import { createRouter, createWebHistory } from 'vue-router'
import { beforeEachGuard, afterEachGuard } from './guards'
import MainLayout from '@/components/Layout/MainLayout.vue'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/',
    redirect: '/dashboard',
    component: () => import('@/components/Layout/MainLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '系统首页', icon: 'House' }
      },
      // 用户管理
      {
        path: '/user/list',
        name: 'UserList',
        component: () => import('@/views/users/UserList.vue'),
        meta: { title: '用户列表', icon: 'User' }
      },
      {
        path: '/user/roles',
        name: 'RoleManagement',
        component: () => import('@/views/users/RoleManagement.vue'),
        meta: { title: '角色管理', icon: 'UserFilled' }
      },
      // 人力资源管理
      {
        path: '/hr/employees',
        name: 'EmployeeList',
        component: () => import('@/views/hr/EmployeeList.vue'),
        meta: { title: '员工档案', icon: 'Avatar' }
      },
      {
        path: '/hr/attendance',
        name: 'AttendanceManagement',
        component: () => import('@/views/hr/AttendanceManagement.vue'),
        meta: { title: '考勤管理', icon: 'Clock' }
      },
      {
        path: '/hr/salary',
        name: 'SalaryManagement',
        component: () => import('@/views/hr/SalaryManagement.vue'),
        meta: { title: '薪资管理', icon: 'Money' }
      },
      {
        path: '/hr/departments',
        name: 'DepartmentManagement',
        component: () => import('@/views/hr/DepartmentManagement.vue'),
        meta: { title: '部门管理', icon: 'OfficeBuilding' }
      },
      // 采购管理
      {
        path: '/purchase/suppliers',
        name: 'SupplierManagement',
        component: () => import('@/views/purchase/SupplierManagement.vue'),
        meta: { title: '供应商管理', icon: 'OfficeBuilding' }
      },
      {
        path: '/purchase/orders',
        name: 'PurchaseOrderManagement',
        component: () => import('@/views/purchase/PurchaseOrderManagement.vue'),
        meta: { title: '采购订单', icon: 'Document' }
      },
      {
        path: '/purchase/inbound',
        name: 'PurchaseInboundManagement',
        component: () => import('@/views/purchase/PurchaseInboundManagement.vue'),
        meta: { title: '采购入库', icon: 'Box' }
      },
      {
        path: '/purchase/statistics',
        name: 'PurchaseStatistics',
        component: () => import('@/views/purchase/PurchaseStatistics.vue'),
        meta: { title: '采购统计', icon: 'DataAnalysis' }
      },
      // 仓库管理
      {
        path: '/warehouse/inventory',
        name: 'InventoryManagement',
        component: () => import('@/views/warehouse/InventoryManagement.vue'),
        meta: { title: '库存管理', icon: 'Box' }
      },
      {
        path: '/warehouse/inbound',
        name: 'InboundManagement',
        component: () => import('@/views/warehouse/InboundManagement.vue'),
        meta: { title: '入库管理', icon: 'Upload' }
      },
      {
        path: '/warehouse/outbound',
        name: 'OutboundManagement',
        component: () => import('@/views/warehouse/OutboundManagement.vue'),
        meta: { title: '出库管理', icon: 'Download' }
      },
      // 店铺管理
      {
        path: '/store/info',
        name: 'StoreInfo',
        component: () => import('@/views/store/StoreInfo.vue'),
        meta: { title: '店铺信息', icon: 'Shop' }
      },
      {
        path: '/store/staff',
        name: 'StaffManagement',
        component: () => import('@/views/store/StaffManagement.vue'),
        meta: { title: '店员管理', icon: 'Avatar' }
      },
      // 盘点管理
      {
        path: '/inventory/plan',
        name: 'InventoryPlan',
        component: () => import('@/views/inventory/InventoryPlan.vue'),
        meta: { title: '盘点计划', icon: 'Calendar' }
      },
      {
        path: '/inventory/execution',
        name: 'InventoryExecution',
        component: () => import('@/views/inventory/InventoryExecution.vue'),
        meta: { title: '盘点执行', icon: 'Operation' }
      },
      {
        path: '/inventory/result',
        name: 'InventoryResult',
        component: () => import('@/views/inventory/InventoryResult.vue'),
        meta: { title: '盘点结果', icon: 'DocumentChecked' }
      },
      {
        path: '/inventory/statistics',
        name: 'InventoryStatistics',
        component: () => import('@/views/inventory/InventoryStatistics.vue'),
        meta: { title: '盘点统计', icon: 'DataAnalysis' }
      },
      // 低代码开发
      {
        path: '/lowcode/forms',
        name: 'FormDesigner',
        component: () => import('@/views/lowcode/FormDesigner.vue'),
        meta: { title: '表单设计器', icon: 'Edit' }
      },
      {
        path: '/lowcode/workflows',
        name: 'WorkflowDesigner',
        component: () => import('@/views/lowcode/WorkflowDesigner.vue'),
        meta: { title: '流程设计器', icon: 'Connection' }
      },
      // 销售管理
      {
        path: '/sales/order',
        name: 'SalesOrder',
        component: () => import('@/views/sales/SalesOrderManagement.vue'),
        meta: { title: '销售订单', icon: 'Document' }
      },
      {
        path: '/sales/customer',
        name: 'CustomerManagement',
        component: () => import('@/views/sales/CustomerManagement.vue'),
        meta: { title: '客户管理', icon: 'User' }
      },
      {
        path: '/sales/contract',
        name: 'SalesContract',
        component: () => import('@/views/sales/SalesContractManagement.vue'),
        meta: { title: '销售合同', icon: 'Document' }
      },
      // 财务管理
      {
        path: '/finance/account',
        name: 'AccountManagement',
        component: () => import('@/views/finance/AccountManagement.vue'),
        meta: { title: '账户管理', icon: 'CreditCard' }
      },
      {
        path: '/finance/invoice',
        name: 'InvoiceManagement',
        component: () => import('@/views/finance/InvoiceManagement.vue'),
        meta: { title: '发票管理', icon: 'Document' }
      },
      {
        path: '/finance/report',
        name: 'FinancialReport',
        component: () => import('@/views/finance/FinancialReport.vue'),
        meta: { title: '财务报表', icon: 'DataAnalysis' }
      },
      // 生产管理
      {
        path: '/production/dashboard',
        name: 'ProductionDashboard',
        component: () => import('@/views/production/ProductionDashboard.vue'),
        meta: { title: '生产管理主页', icon: 'Operation' }
      },
      {
        path: '/production/planning',
        name: 'ProductionPlanning',
        component: () => import('@/views/production/ProductionPlanning.vue'),
        meta: { title: '生产计划管理', icon: 'Calendar' }
      },
      {
        path: '/production/workorder',
        name: 'WorkOrderManagement',
        component: () => import('@/views/production/WorkOrderManagement.vue'),
        meta: { title: '工单管理', icon: 'Document' }
      },
      {
        path: '/production/scheduling',
        name: 'ProductionScheduling',
        component: () => import('@/views/production/ProductionScheduling.vue'),
        meta: { title: '生产调度', icon: 'Timer' }
      },
      {
        path: '/production/process',
        name: 'ProcessRouteManagement',
        component: () => import('@/views/production/ProcessRouteManagement.vue'),
        meta: { title: '工艺路线管理', icon: 'Connection' }
      },
      {
        path: '/production/quality',
        name: 'QualityManagement',
        component: () => import('@/views/production/QualityManagement.vue'),
        meta: { title: '质量管理', icon: 'Medal' }
      },
      {
        path: '/production/equipment',
        name: 'EquipmentManagement',
        component: () => import('@/views/production/EquipmentManagement.vue'),
        meta: { title: '设备管理', icon: 'Tools' }
      },
      {
        path: '/production/reports',
        name: 'ProductionReports',
        component: () => import('@/views/production/ProductionReports.vue'),
        meta: { title: '生产报表', icon: 'DataAnalysis' }
      },
      // 基础档案管理
      {
        path: '/basic/product',
        name: 'ProductManagement',
        component: () => import('@/views/basic/ProductManagement.vue'),
        meta: { title: '商品档案', icon: 'Goods' }
      },
      {
        path: '/basic/supplier',
        name: 'BasicSupplierManagement',
        component: () => import('@/views/basic/SupplierManagement.vue'),
        meta: { title: '供应商档案', icon: 'OfficeBuilding' }
      },
      {
        path: '/basic/customer',
        name: 'BasicCustomerManagement',
        component: () => import('@/views/basic/CustomerManagement.vue'),
        meta: { title: '客户档案', icon: 'User' }
      },
      {
        path: '/basic/warehouse',
        name: 'BasicWarehouseManagement',
        component: () => import('@/views/basic/WarehouseManagement.vue'),
        meta: { title: '仓库档案', icon: 'House' }
      },
      {
        path: '/basic/bom',
        name: 'BomManagement',
        component: () => import('@/views/basic/BomManagement.vue'),
        meta: { title: 'BOM管理', icon: 'List' }
      },
      {
        path: '/basic/material',
        name: 'MaterialManagement',
        component: () => import('@/views/basic/MaterialManagement.vue'),
        meta: { title: '物料管理', icon: 'Box' }
      },
      // 批发管理
      {
        path: '/wholesale/order',
        name: 'WholesaleOrderManagement',
        component: () => import('@/views/wholesale/WholesaleOrderManagement.vue'),
        meta: { title: '批发订单', icon: 'Document' }
      },
      {
        path: '/wholesale/customer',
        name: 'WholesaleCustomerManagement',
        component: () => import('@/views/wholesale/WholesaleCustomerManagement.vue'),
        meta: { title: '批发客户', icon: 'User' }
      },
      {
        path: '/wholesale/price',
        name: 'WholesalePriceManagement',
        component: () => import('@/views/wholesale/WholesalePriceManagement.vue'),
        meta: { title: '批发价格', icon: 'Money' }
      },
      {
        path: '/wholesale/statistics',
        name: 'WholesaleStatistics',
        component: () => import('@/views/wholesale/WholesaleStatistics.vue'),
        meta: { title: '批发统计', icon: 'DataAnalysis' }
      },
      // 配货管理
      {
        path: '/distribution/plan',
        name: 'DistributionPlan',
        component: () => import('@/views/distribution/DistributionPlan.vue'),
        meta: { title: '配货计划', icon: 'MapLocation' }
      },
      {
        path: '/distribution/order',
        name: 'DistributionOrder',
        component: () => import('@/views/distribution/DistributionOrder.vue'),
        meta: { title: '配货单管理', icon: 'Document' }
      },
      {
        path: '/distribution/vehicle',
        name: 'VehicleManagement',
        component: () => import('@/views/distribution/VehicleManagement.vue'),
        meta: { title: '车辆管理', icon: 'Truck' }
      },
      {
        path: '/distribution/statistics',
        name: 'DistributionStatistics',
        component: () => import('@/views/distribution/DistributionStatistics.vue'),
        meta: { title: '配送统计', icon: 'DataAnalysis' }
      },
      // 零售管理
      {
        path: '/retail/order',
        name: 'RetailOrderManagement',
        component: () => import('@/views/retail/RetailOrderManagement.vue'),
        meta: { title: '零售订单', icon: 'ShoppingCart' }
      },
      {
        path: '/retail/member',
        name: 'MemberManagement',
        component: () => import('@/views/retail/MemberManagement.vue'),
        meta: { title: '会员管理', icon: 'User' }
      },
      {
        path: '/retail/promotion',
        name: 'PromotionManagement',
        component: () => import('@/views/retail/PromotionManagement.vue'),
        meta: { title: '促销活动', icon: 'Promotion' }
      },
      {
        path: '/retail/statistics',
        name: 'RetailStatistics',
        component: () => import('@/views/retail/RetailStatistics.vue'),
        meta: { title: '零售统计', icon: 'DataAnalysis' }
      },
      // 报表中心
      {
        path: '/report/sales',
        name: 'SalesReport',
        component: () => import('@/views/report/SalesReport.vue'),
        meta: { title: '销售报表', icon: 'TrendCharts' }
      },
      {
        path: '/report/finance',
        name: 'FinanceReport',
        component: () => import('@/views/report/FinanceReport.vue'),
        meta: { title: '财务报表', icon: 'Money' }
      },
      {
        path: '/report/inventory',
        name: 'InventoryReport',
        component: () => import('@/views/report/InventoryReport.vue'),
        meta: { title: '库存报表', icon: 'Box' }
      },
      {
        path: '/report/purchase',
        name: 'PurchaseReport',
        component: () => import('@/views/report/PurchaseReport.vue'),
        meta: { title: '采购报表', icon: 'Document' }
      },
      {
        path: '/report/pivot',
        name: 'PivotReport',
        component: () => import('@/views/report/PivotReport.vue'),
        meta: { title: '数据透视报表', icon: 'Grid' }
      },
      // 项目管理
      {
        path: '/project/dashboard',
        name: 'ProjectDashboard',
        component: () => import('@/views/project/ProjectDashboard.vue'),
        meta: { title: '项目管理主页', icon: 'Management' }
      },
      {
        path: '/project/initiation',
        name: 'ProjectInitiation',
        component: () => import('@/views/project/ProjectInitiation.vue'),
        meta: { title: '项目立项', icon: 'Plus' }
      },
      {
        path: '/project/planning',
        name: 'ProjectPlanning',
        component: () => import('@/views/project/ProjectPlanning.vue'),
        meta: { title: '项目计划', icon: 'Calendar' }
      },
      {
        path: '/project/resource',
        name: 'ResourceManagement',
        component: () => import('@/views/project/ResourceManagement.vue'),
        meta: { title: '资源管理', icon: 'User' }
      },
      {
        path: '/project/progress',
        name: 'ProgressTracking',
        component: () => import('@/views/project/ProgressTracking.vue'),
        meta: { title: '进度跟踪', icon: 'TrendCharts' }
      },
      {
        path: '/project/cost',
        name: 'CostControl',
        component: () => import('@/views/project/CostControl.vue'),
        meta: { title: '成本控制', icon: 'Money' }
      },
      {
        path: '/project/risk',
        name: 'RiskManagement',
        component: () => import('@/views/project/RiskManagement.vue'),
        meta: { title: '风险管理', icon: 'Warning' }
      },
      {
        path: '/project/document',
        name: 'DocumentManagement',
        component: () => import('@/views/project/DocumentManagement.vue'),
        meta: { title: '文档管理', icon: 'Document' }
      },
      {
        path: '/project/reports',
        name: 'ProjectReports',
        component: () => import('@/views/project/ProjectReports.vue'),
        meta: { title: '项目报表', icon: 'DataAnalysis' }
      },
      // 系统管理
      {
        path: '/system/user',
        name: 'SystemUser',
        component: () => import('@/views/system/UserManagement.vue'),
        meta: { title: '用户管理', icon: 'User' }
      },
      {
        path: '/system/role',
        name: 'SystemRole',
        component: () => import('@/views/system/RoleManagement.vue'),
        meta: { title: '角色管理', icon: 'UserFilled' }
      },
      {
        path: '/system/permission',
        name: 'SystemPermission',
        component: () => import('@/views/system/PermissionManagement.vue'),
        meta: { title: '权限管理', icon: 'Lock' }
      },
      {
        path: '/system/config',
        name: 'SystemConfig',
        component: () => import('@/views/system/SystemConfig.vue'),
        meta: { title: '系统配置', icon: 'Setting' }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/Error/404.vue'),
    meta: { title: '页面不存在' }
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 注册路由守卫
router.beforeEach(beforeEachGuard)
router.afterEach(afterEachGuard)

export default router
